<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
 
# ONLY WITH PEAR SUPPORT
require(ROOT_PATH."includes/Tar.php");
include_once(ROOT_PATH."class/upload.class.php");
include_once(ROOT_PATH."class/user.class.php");
include_once(ROOT_PATH."class/team.class.php");
include_once(ROOT_PATH."class/files.class.php");
include_once(ROOT_PATH."class/project.class.php");
include_once(ROOT_PATH."func/deltree.inc.php");

define("CHECKPO",ROOT_PATH."bash/checkupload.sh");

@$ftmp = $_FILES['fileup']['tmp_name'];
@$oname = $_FILES['fileup']['name'];
@$fname = $_FILES['fileup']['name'];
@$fsize = $_FILES['fileup']['size'];
@$ftype = $_FILES['fileup']['type'];

include_once(ROOT_PATH."includes/currentuser.php");

if(isset($ftmp)) {
	$fileid = util::hashcode($fname.$CURRENT_USER->userid.$fsize);

	$new_filename = UPLOAD_PATH."/".$fileid;
	$resultsfile = $new_filename."_results";
	
	if (!file_exists(UPLOAD_PATH)) {
		mkdir(UPLOAD_PATH);
	}
	
	move_uploaded_file($ftmp,$new_filename);

	$tar_path = TEMP_PATH."/".$fileid;

	if (file_exists($tar_path)) {
		debug($tar_path. " exists. Deltree.");
		deltree($tar_path);
	} 
	mkdir($tar_path);

	$tar = new Archive_Tar($new_filename);
	
	if ($tar->extract($tar_path)) {
		
		if ($islocal===true) {
			$resultsfile="c:\\xampp\\htdocs\\file.txt";
		} else {
			exec(CHECKPO." ".$tar_path." ".$resultsfile); // FOR PEAR	
		}
		
		if (file_exists($resultsfile)) {
			$result = file($resultsfile);
			if ($islocal!==true) {
				unlink($resultsfile);
			}
			debug($result);
			
			if (count($result)>0) {
		
				$upload = new upload;
				$upload->id=$fileid;
				$upload->userid=$CURRENT_USER->userid;
				$upload->projectid=$_SESSION['projectid'];
				$upload->teamid=$CURRENT_USER->teamid;
				$upload->fname=strtolower(APPLICATION_NAME."_".util::now()."_".$CURRENT_USER->userid.".tar.gz");
				$upload->fsize=filesize($new_filename);
	
				$addfiles = array();
				$fileres = array();
				
				$team = new team;
				$team->id = $CURRENT_USER->teamid;
				$loader->get($team);
				
				foreach ($result as $rs) {
					$status="";
					$statustext="";
					$r = explode(":",$rs);
					$fileprj = "-";
					if (trim($r[1]) == "1") {
						$file = new files($_SESSION['projectid']);
						$file->name=trim($r[0]);
						$file->teamid=$CURRENT_USER->teamid;
						$list = $loader->loadListBy($file,$file->findFile());
						debug("List count: ".count($list));
						if (count($list)>0) {
							foreach ($list as $file) {
								$fileprj=$file->path;
								if ($file->userid == $CURRENT_USER->userid && ($file->statusid==FILE_STATUS_LOCKED or $file->statusid==FILE_STATUS_PROOF)) {
									$upload->addFile($file->id);
									
									$prj = new project;
									$prj->id=$file->subprj;
									$loader->get($prj);
									$targetpath = $tar_path.str_replace('$LANG',$team->lang,$prj->path);
									if (!file_exists($targetpath)) {
										mkdir($targetpath,0777,true);
									}
									
									rename($tar_path."/".$file->name,$targetpath."/".$file->name);									
									array_push($addfiles,$targetpath."/".$file->name);
									$status="ok";
									break;
								}
							}
							if ($status!="ok") {
								$status="notassigned";
							}				
						} else {
							$status="notfound";
						}
					} else {
						$status="notok";
						$statusfile=$tar_path."/".str_replace("\n","",$r[2]);
						$statustext=str_replace("'","`",file_get_contents($statusfile));
						unlink($statusfile);
					}
					$ar = array("file" => $r[0],"status" => $status,"prj" => $fileprj,"statustext" => $statustext);
					array_push($fileres,$ar);
				}
		
				if (count($upload->filesid)>0) {
					$loader->save($upload);	
				}
				
				if (count($addfiles)>0) {
					unset($tar);
					unlink($new_filename);
					$tar = new Archive_Tar($new_filename,"gz");
					$tar->createModify($addfiles,"",$tar_path);
				}
				$tm->assign("canconfirm",count($addfiles)>0 ? "yes" : "no");
				$tm->assign("fileid",$fileid);
				$tm->assign("list",$fileres);
				$content = $tm->fetch("uploadresult.tpl");
			}	
			
		}
	}
	deltree($tar_path);
} else {
	$error = $LANG['uploaderror'];
	$content=$tm->fetch("fileupload.tpl");
	//echo $LANG['uploaderror']." ["."<span class=\"link\" onclick=\"upAction('fileupload')\">".$LANG['retry']."</span>]";	
}

?>
